package com.trg.generaltool.util.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: ThreadPool
 * Description:
 * date: 2020-10-23 17:29
 *
 * @author tangrg
 * @since JDK 1.8
 */
public class ThreadPool {

    // 线程数
    public static final int THREAD_POOL_SIZE = 16;

    public static void main(String[] args) throws InterruptedException {
        String s = excThead();
        System.out.println(s);
        // 任务执行完毕后打印"Done"
        System.out.println("Done");
    }


    public static String excThead() throws InterruptedException {
        StringBuilder sb = new StringBuilder();
        // 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("pool-%d").build();

        // 创建线程池，其中任务队列需要结合实际情况设置合理的容量
        ThreadPoolExecutor executor = new ThreadPoolExecutor(THREAD_POOL_SIZE,
                THREAD_POOL_SIZE,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024),
                namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());

        // 新建 1000 个任务，每个任务是打印当前线程名称
        for (int i = 0; i < 100; i++) {
            executor.execute(() -> System.out.println(Thread.currentThread().getName()));
            executor.execute(new Calc(i, i + 1));
            sb.append(i);
        }
        // 优雅关闭线程池
        executor.shutdown();
        executor.awaitTermination(1000L, TimeUnit.SECONDS);
        return sb.toString();
    }
}
